package com.mmlc.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.BrandListRequestFilter;
import com.mmlc.network.responsebean.BrandListResponseBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/9/30.
 */
public class BrandView extends LinearLayout {


    private String brandId;

    private String brandName;
    private ImageView ivback;
    private RelativeLayout rltitle;
    private TextView tvchild;
    private ImageView ivcheck;
    private BladeListView bladelistview;


    @TargetApi(21)
    public BrandView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    public BrandView(Context context) {
        super(context);
        initView();
    }

    public BrandView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BrandView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_brand, this, true);
        ivback = (ImageView) findViewById(R.id.iv_back);
        rltitle = (RelativeLayout) findViewById(R.id.rl_title);
        tvchild = (TextView) findViewById(R.id.tv_child);
        tvchild.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                brandId = "";
                brandName = "";
                refreView();
                animOutView();
                if (childClickedListener != null) {
                    childClickedListener.childClicked(brandId, brandName);
                }
            }
        });
        ivcheck = (ImageView) findViewById(R.id.iv_check);
        bladelistview = (BladeListView) findViewById(R.id.blade_listview);
        bladelistview.setSelect(true);
        setVisibility(View.GONE);
        ivback.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                animOutView();
            }
        });
        bladelistview.setChildClickedListener(new BladeListView.ChildClickedListener() {
            @Override
            public void childClicked(String brandId, String brandName) {
                BrandView.this.brandId = brandId;
                BrandView.this.brandName = brandName;
                refreView();
                if (childClickedListener != null)
                    childClickedListener.childClicked(brandId, brandName);
                animOutView();
            }
        });
//        getData("");
    }

    public ChildClickedListener childClickedListener;

    public interface ChildClickedListener {
        public void childClicked(String brandId, String brandName);
    }

    public void setChildClickedListener(ChildClickedListener childClickedListener) {
        this.childClickedListener = childClickedListener;
    }

    public void setData(String brandId, String brandName) {
        this.brandId = brandId;
        this.brandName = brandName;
        refreView();
    }

    private void refreView() {
        if (datas != null) {
            if (TextUtils.isEmpty(brandId)) {
                ivcheck.setVisibility(View.VISIBLE);
            } else {
                ivcheck.setVisibility(View.GONE);
            }
            bladelistview.adapter.setBrandId(brandId);
            bladelistview.adapter.notifyDataSetChanged();
        }
    }


    public void animInView() {
        setVisibility(View.VISIBLE);
        if (getMeasuredWidth() == 0)
            return;
        final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "x", getMeasuredWidth(), 0f);
        objectAnimator.setDuration(300);
        x.task().post(new Runnable() {
            @Override
            public void run() {
                objectAnimator.start();
            }
        });


    }

    public void animOutView() {
        if (getMeasuredWidth() == 0)
            return;
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "x", 0f, getMeasuredWidth());
        objectAnimator.setDuration(300);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        objectAnimator.start();
    }

    List<BrandListResponseBean.DataBean> datas;

    public void getData(String keyword) {
        BrandListRequestFilter filter = new BrandListRequestFilter((BaseParentActivity) getContext());
        filter.requestBean.method = "brandList";
        filter.requestBean.keywords=keyword;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BrandListResponseBean>() {
            @Override
            public void onSuccess(BrandListResponseBean result) {
                if (result == null) {
                    result = new BrandListResponseBean();
                }

                if (result.getData() != null) {
                    datas = result.getData();
                    bladelistview.setDatas(datas);
                    refreView();
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }
//    public void getData(String cateId) {
//        BrandListRequestFilter filter = new BrandListRequestFilter((BaseParentActivity) getContext());
//        filter.requestBean.method = "brandList";
//        filter.requestBean.cate_id = cateId;
//        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<BrandListResponseBean>() {
//            @Override
//            public void onSuccess(BrandListResponseBean result) {
//                if (result == null) {
//                    result = new BrandListResponseBean();
//                }
//
//                if (result.getData() != null) {
//                    datas = result.getData();
//                    bladelistview.setDatas(datas);
//                    refreView();
//                }
//            }
//
//            @Override
//            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
//
//            }
//        });
//    }

//    public void  getSxData(String pid,String brand_id,String keywords){
//        SXBrandListRequestFilter sxBrandListRequestFilter=new SXBrandListRequestFilter((BaseParentActivity) getContext());
//        sxBrandListRequestFilter.requestBean.pid=pid;
//        sxBrandListRequestFilter.requestBean.keywords=keywords;
//        sxBrandListRequestFilter.requestBean.brand_id=brand_id;
//        sxBrandListRequestFilter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<SXBrandListResponseBean>() {
//            @Override
//            public void onSuccess(SXBrandListResponseBean result) {
//                if (result == null) {
//                    result = new SXBrandListResponseBean();
//                }
//
//                if (result.getData() != null) {
//                    datas = result.getData();
//                    bladelistview.setDatas(datas);
//                    refreView();
//                }
//            }
//
//            @Override
//            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
//
//            }
//        });
//    }


}
